package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.dy.nygl.pojo.ElecStatus;
import com.dy.nygl.service.ElecStatusService;

import com.uwantsoft.goeasy.client.goeasyclient.GoEasy;

@Controller
@RequestMapping("/elecStatus")
public class ElecStatusController {

	@Resource(name = "elecStatusService")
	private ElecStatusService elecStatusService;

	private List<ElecStatus> elecList;

	private ElecStatus elecStatus1;
	
	
	
	public ElecStatus getElecStatus1() {
		return elecStatus1;
	}

	public void setElecStatus1(ElecStatus elecStatus1) {
		this.elecStatus1 = elecStatus1;
	}

	@RequestMapping("/getList.action")
	public ModelAndView getAll(){
		elecList=elecStatusService.selectAll();
		ModelAndView model=new ModelAndView();
		model.addObject("elecList", elecList);
		model.setViewName("equipmoni");
		return model;
	
	}
	
	@RequestMapping("/selectById.action")
	public void selectById(@Param("id")int id,HttpServletResponse response)throws IOException{
		
		elecStatus1=elecStatusService.selectById(id);
		setElecStatus1(elecStatus1);
		PrintWriter out=response.getWriter();
		out.print(8);
		
	}
	
	@RequestMapping("/returnView.action")
	public ModelAndView returnView(){
		
		ModelAndView model=new ModelAndView();
		elecStatus1=getElecStatus1();
		model.addObject("elecStatus1", elecStatus1);
		model.setViewName("equipmoni");
		return model;
	}
	@RequestMapping("/selectByName.action")
	public ModelAndView selectByName(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
	/*	elecList=elecStatusService.selectByName("设备1-A");*/
		String id1 = request.getParameter("id");
		int id = Integer.parseInt(id1);
		String name = request.getParameter("name");
		String status = request.getParameter("status");
		String data = request.getParameter("data");

		ElecStatus e2 = new ElecStatus();
		e2.setId(id);
		e2.setName(name.trim());
		e2.setStatus(status.trim());
		e2.setData(data.trim());

		String json = JSON.toJSONString(e2);

		/*ElecStatus e = JSON.parseObject(json, ElecStatus.class);
		System.out.println(e.getData());*/

		System.err.println("设备传递过来的json数据：" + json);
		GoEasy goEasy = new GoEasy("10a2c88a-15dc-4173-abe6-44c032543ca8");
		goEasy.publish("666666", json);
		
		ModelAndView model=new ModelAndView();
		model.addObject("jsonData", json);
		model.setViewName("showjson");
		return model;
	}

	/* [{"data":"1799","id":1,"name":"设备1-A","status":"开启"}] */
	/* {'data':'1799','id':1,'name':'设备1-A','status':'开启'} */
	@RequestMapping("/updateData.action")
	public void updateData(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String p = request.getParameter("p");
		ElecStatus e = JSON.parseObject(p, ElecStatus.class);
		String name = e.getName();
		String status = e.getStatus();
		String data = e.getData();
		elecStatusService.updateInfo(e);
		System.err.println("------数据库设备信息更新成功----Success Update-----------");
		PrintWriter out = response.getWriter();
		out.print(9);

	}

	@RequestMapping("/getMQ.action")
	public void getMQ() {
		System.err.println("Send Message...");
		GoEasy goEasy = new GoEasy("10a2c88a-15dc-4173-abe6-44c032543ca8");
		goEasy.publish("666666", "Hello");
	}

}